<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet"  th:href="@{lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="demoTable">
            用户名
            <div class="layui-inline">
                <input class="layui-input" name="username" id="username" autocomplete="off">
            </div>
            电话
            <div class="layui-inline">
                <input class="layui-input" name="tel" id="tel" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">高级查询</button>
        </div>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" shiro:hasPermission="user:insert"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete" shiro:hasPermission="user:delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">
			
		</table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"shiro:hasPermission="user:update">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"shiro:hasPermission="user:delete">删除</a>
        </script>

    </div>
</div>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: 'user/userAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 100, title: 'ID', sort: true},
                {field: 'username', width: 100, title: '用户名'},
				{field: 'realname', width: 100, title: '真实姓名'},
				{field: 'dname', width: 100, title: '所在部门'},
                {field: 'sex', width: 100, title: '性别', sort: true},
                {field: 'tel', width: 120, title: '手机号码'},
                {field: 'status', title: '用户状态', minWidth: 150},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            id:'testReload'
        });
        //高级查询操作 直接在前端的数据搜索 不用提交给后台查询
        var $ = layui.$, active = {
            reload: function(){
                //用户名
                var usernameId=$('#username');
                var telId=$('#tel');//电话
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        username:usernameId.val().trim(),
                        tel:telId.val().trimEnd()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    content: 'addUser',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                /*var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));*/
            //    选中信息
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                console.log(data);
                if (data.length == 0 ){
                    layer.msg("请选择要删除的记录信息");
                }else{ //删除工作
                    //获取ids
                    var ids=getCheckId(data);
                    console.log(ids);
                    layer.confirm('你真的要删除吗？',function (index) {
                        deleteUserByIds(ids,index);
                    })
                }
            }
        });

        /**
         * 获取选中id的集合
         */
        function getCheckId(data){
            var arr=new Array();
            for(var i=0;i<data.length;i++){
                arr.push(data[i].id);
            }
            //拼接id记录
            return arr.join(",");
        }

        /**
         * 根据id删除记录信息
         */
        function deleteUserByIds(ids,index){
            //向后台发送数据并进行添加操作
            $.ajax({
                url:"user/deleteUserByIds",
                type:"POST",
                data:{'ids':ids},
                success:function(result){
                    if(result.code==200){
                        layer.msg("删除成功",{
                            icon:6,
                            time:500
                        },function(){
                            parent.window.location.reload();//重新页面
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });

                    }else{
                        layer.msg("删除失败");
                    }
                }
            })
            return false;
        }


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'updateUser?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //调用删除函数，获取当前行的id
                    deleteUserByIds(data.id,index);
                });
            }
        });

    });
</script>

</body>
</html>